package csc.fresher.finalgroupfour.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import csc.fresher.finalgroupfour.domain.Account;
import csc.fresher.finalgroupfour.domain.Transaction;
import csc.fresher.finalgroupfour.domain.User;
import csc.fresher.finalgroupfour.service.AccountService;
import csc.fresher.finalgroupfour.service.LoggingService;
import csc.fresher.finalgroupfour.service.StateService;
import csc.fresher.finalgroupfour.service.TransactionService;
import csc.fresher.finalgroupfour.service.UserService;

@Controller
public class TransactionController {
	@Autowired
	private UserService userService;

	@Autowired
	private TransactionService transactionService;

	@Autowired
	private AccountService accountService;

	@Autowired
	private StateService stateService;
	
	@Autowired
	private LoggingService logService;

	@RequestMapping(value = "/viewTransaction")
	public ModelAndView viewTransaction() {
		ModelAndView model = new ModelAndView("viewTransaction");
		String loginId = userService.getLoginId();
		User user = userService.findUser(loginId);
		
		List<Account> listAccounts = (List<Account>) user.getAccounts();
		model.addObject("listAccounts", listAccounts);

		List<Transaction> listTransaction = transactionService.listTransactions(user);
		model.addObject("listTransactions", listTransaction);
		return model;
	}

	/**
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/requestTransaction")
	public String requestTransaction(
			@RequestParam("sourceAccount") String sourceAccount,
			@RequestParam("targetAccount") String targetAccount,
			@RequestParam("amount") String amountNumber, Model model) {
		long amount = (long) Double.parseDouble(amountNumber);
		Account srcAcc = accountService.findAccount(sourceAccount);
		Account desAcc = accountService.findAccount(targetAccount);

		if (srcAcc == null || desAcc == null) {
			model.addAttribute("errorMessage", "Account not found");
			return "forward:/viewTransaction";
		}
		
		boolean isSuccess = transactionService.makeNewTransaction(srcAcc, desAcc, amount);
		if (!isSuccess) {
			model.addAttribute("errorMessage", "Not enough money");
		}
		
		return "forward:/viewTransaction";
	}

	@RequestMapping(value = { "adminApproveTransaction" })
	public String approveTransaction(
			@RequestParam("state") String transactionState,
			@RequestParam("index") String index, Model model) {

		List<Transaction> listTransaction = transactionService
				.listTransactions();
		Transaction transaction = listTransaction.get(Integer.parseInt(index));
		transactionService.approveTransaction(transaction, transactionState);
		return "redirect:/viewTransaction";
	}
}
